package com.css.zfzx.fxpg.common.utils.cluster;

import com.css.zfzx.fxpg.modules.buildings.data.repository.entity.FxBuildingsEntity;

import java.util.ArrayList;
import java.util.List;

public class ObjectTransformerUtil {

    public static List<FxBuildingsEntity> getClustersPEDEntities (List<FxBuildingsEntity> entities, String coordinate, int zoom) {
        String[] coords = coordinate.split(",");
        String[] coord0 = coords[0].split(" ");
        Point sw = new Point(Double.parseDouble(coord0[0]), Double.parseDouble(coord0[1]));
        String[] coord1 = coords[1].split(" ");
        Point ne = new Point(Double.parseDouble(coord1[0]), Double.parseDouble(coord1[1]));
        Bounds bounds = new Bounds(sw, ne);

        List<Marker> markers = pedentities2markers(entities);
        MarkerClusterer markerClusterer = new MarkerClusterer(markers, bounds, zoom);
        List<FxBuildingsEntity> result = clusters2pedentities(markerClusterer.clusters);
        return result;
    }

    private static List<Marker> pedentities2markers (List<FxBuildingsEntity> entities) {
        int size = entities.size();
        List<Marker> markers = new ArrayList<>(size);
        FxBuildingsEntity entity = null;
        for (int i = 0; i < size; i++) {
            entity = entities.get(i);
            Marker marker = new Marker();
            marker.id = entity.getId();
            Point position = new Point(Double.parseDouble(entity.getLon()), Double.parseDouble(entity.getLat()));
            marker.position = position;
            markers.add(marker);
        }
        return markers;
    }

    private static List<FxBuildingsEntity> clusters2pedentities (List<Cluster> clusters) {
        int size = clusters.size();
        List<FxBuildingsEntity> entities = new ArrayList<>(size);
        Cluster cluster = null;
        Point position = null;
        for (int i = 0; i < clusters.size(); i++) {
            cluster = clusters.get(i);
            position = cluster.center;
            FxBuildingsEntity entity = new FxBuildingsEntity();
            entity.setId(cluster.id);
            entity.setLon(String.valueOf(position.lng));
            entity.setLat(String.valueOf(position.lat));
            entity.setExtend1(String.valueOf(cluster.count));//扩展字段被占用
            entities.add(entity);
        }
        return entities;
    }



    /////////////////////////////////////////////////////////////////////////////////////////

    public static List<FxBuildingsEntity> getClustersRLMPEntities (List<FxBuildingsEntity> entities, String coordinate, int zoom) {
        String[] coords = coordinate.split(",");
        String[] coord0 = coords[0].split(" ");
        Point sw = new Point(Double.parseDouble(coord0[0]), Double.parseDouble(coord0[1]));
        String[] coord1 = coords[1].split(" ");
        Point ne = new Point(Double.parseDouble(coord1[0]), Double.parseDouble(coord1[1]));
        Bounds bounds = new Bounds(sw, ne);

        List<Marker> markers = rmlpentities2markers(entities);
        MarkerClusterer markerClusterer = new MarkerClusterer(markers, bounds, zoom);
        List<FxBuildingsEntity> result = clusters2rmlpentities(markerClusterer.clusters);
        return result;
    }

    private static List<Marker> rmlpentities2markers(List<FxBuildingsEntity> entities) {
        int size = entities.size();
        List<Marker> markers = new ArrayList<>(size);
        FxBuildingsEntity entity = null;
        for (int i = 0; i < size; i++) {
            entity = entities.get(i);
            Marker marker = new Marker();
            marker.id = entity.getId();
            Point position = new Point(Double.parseDouble(entity.getLon()), Double.parseDouble(entity.getLat()));
            marker.position = position;
            markers.add(marker);
        }
        return markers;
    }

    private static List<FxBuildingsEntity> clusters2rmlpentities (List<Cluster> clusters) {
        int size = clusters.size();
        List<FxBuildingsEntity> entities = new ArrayList<>(size);
        Cluster cluster = null;
        Point position = null;
        for (int i = 0; i < clusters.size(); i++) {
            cluster = clusters.get(i);
            position = cluster.center;
            FxBuildingsEntity entity = new FxBuildingsEntity();
            entity.setId(cluster.id);
            entity.setLon(String.valueOf(position.lng));
            entity.setLat(String.valueOf(position.lat));
            entity.setExtend1(String.valueOf(cluster.count));//扩展字段被占用
            entities.add(entity);
        }
        return entities;
    }




}
